import axios from 'axios'
const LOGIN ='LOGIN'
const LOGINOUT='LOGINOUT'
const USER_DATA='USER_DATA'


const initState={
    isAuth:false,
    user:'李云龙',
    age:20
}
export function auth(state=initState,action) {
    console.log(state,action)
    switch (action.type) {
        case LOGIN:
            return {...state,isAuth:true};
            break;
        case LOGINOUT:
            return {...state,isAuth:false};
            break;
        case USER_DATA:
            return {...state,...action.payload};
            break;
        default:
            return state;
            break;
    }
}
export function getUserData() {
    //dispatch用来通知数据修改
    return dispatch=>{
        axios.get('/data').then(res=>{
            if(res.status===200){
                dispatch(userData(res.data))
                // this.setState({data:res.data})
            }
        })
    }
}
export function userData(data) {
    return {type:USER_DATA,payload:data}
}
export function login() {
    return {type:LOGIN}
}
export function loginout() {
    return {type:LOGINOUT}
}